package main

import (
	"github.com/minio/minio-go"
	"fmt"
	"time"
)




func main()  {

	// Use a secure connection.
	ssl := false


	endpoint := "192.168.99.100:9000"
	accessKey := "7LSRF4CCX657DV22HSCX"
	secretAccessKey := "z9QMTzsWIpeCFuYM3GP9yAvnnluUMsjmdmoKr0ST"

	bucketName := "bucket1"
	objectName := "d4.mp4"





	// Initialize minio client object.
	minioClient, err := minio.New(endpoint, accessKey, secretAccessKey, ssl)
	if err != nil {
		fmt.Println(err)
		return
	}

	/*
	filePath := "/Users/zhangxu/Movies/d4.mp4"

	// upload file
	n, err := minioClient.FPutObject(bucketName, objectName, filePath, "application/file")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("n=", n)
*/

	/*
	// Generates a url which expires in a day.
	expiry := time.Second * 24 * 60 * 60 // 1 day.
	presignedURL, err := minioClient.PresignedPutObject(bucketName, objectName, expiry)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(presignedURL)
*/

	//
	policy := minio.NewPostPolicy()
	policy.SetBucket(bucketName)
	policy.SetKey(objectName)
	policy.SetExpires(time.Now().UTC().AddDate(0, 0, 10)) // expires in 10 days

	// Only allow 'png' images.
	policy.SetContentType("image/png")

	// Only allow content size in range 1KB to 1MB.
	policy.SetContentLengthRange(1024, 1024*1024)

	// Get the POST form key/value object:

	url, formData, err := minioClient.PresignedPostPolicy(policy)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("url:", url)
	for k, v := range formData {
		fmt.Println("%s = %s ", k, v)
	}


	fmt.Printf("curl ")
	for k, v := range formData {
		fmt.Printf("-F %s=%s ", k, v)
	}
	fmt.Printf("-F file=@/etc/bash.bashrc ")
	fmt.Printf("%s\n", url)

}
